﻿using Microsoft.Win32;
using StoreProject.OtherEntity.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.OtherUtil
{
    public class RegistryUtil
    {
        public static readonly string KEY_OPD = @"SOFTWARE\LINGTONG";
        public static readonly string FIELD_SERVER_ADDRESS = "ServerAddress";
        public static readonly string FIELD_DB_DATABASE = "DB";

        public static readonly string FIELD_SERVER_PORT = "Port";
        public static readonly string FIELD_DB_SERVER = "DBServer";
        public static readonly string FIELD_DB_USER = "uid";
        public static readonly string FIELD_DB_PSW = "psw";
        public static bool SaveDbConfig(string serverIP, string db, string user, string psw)
        {
            bool success = true;
            try
            {
                SetKeyValue(KEY_OPD, FIELD_SERVER_ADDRESS, serverIP, true);
                SetKeyValue(KEY_OPD, FIELD_DB_DATABASE, db, true);
                SetKeyValue(KEY_OPD, FIELD_DB_USER, DataProtector.EncryptLocalMachine(user, DataProtector.SALT), true);
                SetKeyValue(KEY_OPD, FIELD_DB_PSW, DataProtector.EncryptLocalMachine(psw, DataProtector.SALT), true);
            }
            catch (Exception ex)
            {
                success = false;
                throw new Exception(ex.Message);
            }
            return success;
        }

        /// <summary>
        /// 设置注册表键的值
        /// </summary>
        /// <param name="keyName">键名称</param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="fieldValue">字段值</param>
        /// <param name="createIfNotExists">如果键不存在，是否创建。true：创建；false：不创建</param>
        public static void SetKeyValue(string keyName, string fieldName, object fieldValue, bool createIfNotExists)
        {
            try
            {
                RegistryKey key = GetSubKeyOnLocalMachine(keyName);
                if (key == null && createIfNotExists)
                {
                    key = CreateKeyOnLocalMachine(keyName);
                }
                if (key != null)
                {
                    key.SetValue(fieldName, fieldValue);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 在注册表的LocalMachine下获取子键
        /// </summary>
        /// <param name="keyName">子键名称</param>
        /// <returns>存在返回该键，不存在返回null</returns>
        public static RegistryKey GetSubKeyOnLocalMachine(string keyName)
        {
            RegistryKey key = null;
            try
            {
                key = Registry.LocalMachine.OpenSubKey(keyName, true);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return key;
        }
        /// <summary>
        /// 在注册表的LocalMachine下创建子键
        /// </summary>
        /// <param name="keyName">子键名称</param>
        /// <returns>创建成功，返回创建的子键；创建不成功，返回null值</returns>
        public static RegistryKey CreateKeyOnLocalMachine(string keyName)
        {
            RegistryKey key = null;
            try
            {
                key = GetSubKeyOnLocalMachine(keyName);
                if (key == null)
                    key = Registry.LocalMachine.CreateSubKey(keyName, RegistryKeyPermissionCheck.Default);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return key;
        }

        /// <summary>
        /// 获取数据库设置
        /// </summary>
        /// <returns></returns>
        public static DBConnSetting GetDbConfig()
        {
            DBConnSetting set = new DBConnSetting();
            try
            {
                set.Server = GetKeyValue(KEY_OPD, FIELD_SERVER_ADDRESS);
                set.DbName = GetKeyValue(KEY_OPD, FIELD_DB_DATABASE);
                string user = GetKeyValue(KEY_OPD, FIELD_DB_USER);
                if (user != string.Empty)
                    set.User = DataProtector.DecryptLocalMachine(user, DataProtector.SALT);
                string psw = GetKeyValue(KEY_OPD, FIELD_DB_PSW);
                if (psw != string.Empty)
                    set.Psw = DataProtector.DecryptLocalMachine(psw, DataProtector.SALT);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return set;
        }

        /// <summary>
        /// 获取注册表键的值
        /// </summary>
        /// <param name="keyName">键名称</param>
        /// <param name="fieldName">字段名称</param>
        /// <returns></returns>
        public static string GetKeyValue(string keyName, string fieldName)
        {
            string keyvalue = string.Empty;
            try
            {
                RegistryKey key = GetSubKeyOnLocalMachine(keyName);
                if (key != null)
                {
                    object temp = key.GetValue(fieldName);
                    if (temp != null)
                    {
                        keyvalue = temp.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return keyvalue;
        }
    }
}
